package com.stockz.equity.charting.painter.indicator;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.List;

import org.springframework.stereotype.Component;

import com.stockz.equity.charting.display.indicator.OrdIndicatorDisplay;
import com.stockz.equity.charting.util.ContextUtil;
import com.stockz.equity.charting.util.DisplayUtil;
import com.stockz.equity.charting.util.MathUtil;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.StockRectangle;
import com.stockz.equity.indicator.ord.OrdSwing;
import com.stockz.equity.indicator.ord.OrdSwing.OrdSwingDirection;
import com.stockz.equity.indicator.ord.OrdSwing.OrdSwingValue;
import com.stockz.equity.pattern.Painter;

@Component
public class OrdIndicatorPainter implements Painter<OrdIndicatorDisplay> {

	private int padding = 5;
	
	@Override
	public void paint(Graphics2D g, OrdIndicatorDisplay indicatorDisplay,
			Rectangle r) {

		StockRectangle rectangle = indicatorDisplay.getRectangle();
		List<OrdSwing> ordSwingList = indicatorDisplay.getOrdSwingList();
		if (rectangle == null || rectangle.isEmpty()
				|| ordSwingList == null || ordSwingList.isEmpty()) {
			return;
		}
		StockChartView stockChartView = (StockChartView)ContextUtil.getApplicationContext().getBean(StockChartView.class);
		StockChartPanelState state = stockChartView.getSelectedStockChart()
		.getStockChartPanel().getStockChartPanelState();
		g.setClip(state.getDrawingRectangle());
		g.setColor(indicatorDisplay.getColor());

		double min = rectangle.getMinimumValueVisible();
		double max = rectangle.getMaximumValueVisible();
		
		
		for(OrdSwing ordSwing : ordSwingList){
			int startX = MathUtil.fitX(
					rectangle.x, 
					state.getTimeUnitSpace(),
					state.getEndIndex(), 
					state.getBarList().indexOf(ordSwing.getStart()));
			int endX = MathUtil.fitX(
					rectangle.x, 
					state.getTimeUnitSpace(),
					state.getEndIndex(), 
					state.getBarList().indexOf(ordSwing.getEnd()));
			int startY = MathUtil.fitY(
					rectangle.y,
					rectangle.y + rectangle.height,
					min,
					max,
					ordSwing.getStart().getValue(indicatorDisplay.getBarValueType()), 
					state.isSemiLog());
			int endY = MathUtil.fitY(
					rectangle.y,
					rectangle.y + rectangle.height,
					min,
					max,
					ordSwing.getEnd().getValue(indicatorDisplay.getBarValueType()), 
					state.isSemiLog());
			g.drawLine(startX, startY, endX, endY);
			
			double h = g.getFont().getStringBounds("A", g.getFontRenderContext()).getHeight();
			//Temporaryly setting it as 1
			//int count = ordSwing.getDetails().size();
			//int count = OrdSwingValue.values().length;
			int count = 1;
			double totalHeight = h*count+(count-1)*padding;
			int topY = (int)((startY+endY)/2 - totalHeight/2);
			Color temp = g.getColor();
			g.setColor(Color.black);
			
			
			int x = 0;
			int y = 0;
			String text = null;
			
			if(ordSwing.getDirection() != null 
					&& OrdSwingDirection.UP.equals(ordSwing.getDirection())){
				y = MathUtil.fitY(
						rectangle.y,
						rectangle.y + rectangle.height,
						min,
						max,
						ordSwing.getEnd().getHigh(), 
						state.isSemiLog());
				y = (int)(y - padding);
				x = endX;
				text = DisplayUtil.format(ordSwing.getEnd().getHigh())+"-"+DisplayUtil.format(ordSwing.getEnd().getVolume());
			}else if(ordSwing.getDirection() != null 
					&& OrdSwingDirection.DOWN.equals(ordSwing.getDirection())){
				y = MathUtil.fitY(
						rectangle.y,
						rectangle.y + rectangle.height,
						min,
						max,
						ordSwing.getEnd().getLow(), 
						state.isSemiLog());
				y = (int)(y + h + padding);
				x = endX;
				text = DisplayUtil.format(ordSwing.getEnd().getHigh())+"-"+DisplayUtil.format(ordSwing.getEnd().getVolume());
			}
			
			if(text != null){
				g.drawString(text, x, y);
			}
			
			Font font = g.getFont();
			g.setFont(font.deriveFont(15f));
			for(int i = 0; i < count; i++){
				//String text = DisplayUtil.format(ordSwing.getValue(OrdSwingValue.values()[i]));
				//text = DisplayUtil.format(ordSwing.getValue(OrdSwingValue.AVERAGE_DAILY_VOLUME_PER_PERCENTAGE_CHANGE));
				text = DisplayUtil.format(ordSwing.getValue(OrdSwingValue.AVERAGE_DAILY_VOLUME));
				//text = String.valueOf(ordSwingList.indexOf(ordSwing));
				double w = g.getFont().getStringBounds(text, g.getFontRenderContext()).getWidth();
				x = (int)((startX+endX)/2 - w/2);
				y = (int)(topY+h*(i+1)+padding*i);
				g.drawString(text, x, y);
			}
			g.setFont(font);
			g.setColor(temp);
		}
	}
}
